package com.im

import android.text.TextUtils
import android.widget.Toast

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation

// 登录窗口
open class LogonDialog {
    private lateinit var mShow: MutableState<Boolean>       // 是否显示
    private var mOnOK: (String, String) -> Unit = {_, _ ->} // 用户单击"确定"按钮的响应事件

    /**
     * 界面布局。
     */
    @Composable
    fun Greeting() {
        // 初始化成员
        mShow = remember {
            mutableStateOf(false)
        }
        var password by remember {
            mutableStateOf("")
        }
        var username by remember {
            mutableStateOf("")
        }
        val usernameFocusRequester = remember {
            FocusRequester()
        }
        val passwordFocusRequester = remember {
            FocusRequester()
        }

        // 布局
        if (mShow.value) {
            val context = LocalContext.current
            AlertDialog(
                onDismissRequest = {},
                text = {
                    Column {
                        // "用户名"编辑框
                        OutlinedTextField(
                            modifier = Modifier
                                .fillMaxWidth()
                                .focusRequester(usernameFocusRequester),
                            label = {
                                Text(context.getString(R.string.IDS_USERNAME))
                            },
                            value = username,
                            onValueChange = {
                                if (it.length <= Const.MAX_USERNAME_LEN) {
                                    username = it
                                }
                            },
                            singleLine = true
                        )
                        // "密码"编辑框
                        OutlinedTextField(
                            modifier = Modifier
                                .fillMaxWidth()
                                .focusRequester(passwordFocusRequester),
                            label = {
                                Text(context.getString(R.string.IDS_PASSWORD))
                            },
                            value = password,
                            onValueChange = {
                                if (it.length <= Const.MAX_PASSWORD_LEN) {
                                    password = it
                                }
                            },
                            singleLine = true,
                            keyboardOptions = KeyboardOptions(
                                keyboardType = KeyboardType.Password,
                                imeAction = ImeAction.Done
                            ),
                            visualTransformation = PasswordVisualTransformation()
                        )
                    }
                },
                confirmButton = {
                    // "确定"按钮
                    TextButton({
                        // 检查用户名是否为空
                        if (TextUtils.isEmpty(username)) {
                            Toast.makeText(context, R.string.IDS_USERNAME_EMPTY, Toast.LENGTH_LONG).show()
                            usernameFocusRequester.requestFocus()
                            return@TextButton
                        }

                        // 检查密码是否为空
                        if (TextUtils.isEmpty(password)) {
                            Toast.makeText(context, R.string.IDS_PASSWORD_EMPTY, Toast.LENGTH_LONG).show()
                            passwordFocusRequester.requestFocus()
                            return@TextButton
                        }

                        // 调用回调
                        mOnOK(username, password)
                        username = ""
                        password = ""

                        // 关闭窗口
                        mShow.value = false
                    }) {
                        Text(context.getString(android.R.string.ok))
                    }
                },
                dismissButton = {
                    TextButton(onClick = {
                        // 关闭窗口
                        mShow.value = false
                    }) {
                        Text(context.getString(android.R.string.cancel))
                    }
                }
            )
        }
    }

    /**
     * 显示。
     * @param onOK  用户单击"确定"按钮的响应方法
     */
    fun show(onOK: (String, String) -> Unit) {
        mOnOK = onOK
        mShow.value = true
    }
}
